package cn.jly.spring.annotation.ioc;

import cn.jly.spring.annotation.ioc.config.MainConfigAutowired;
import cn.jly.spring.annotation.ioc.dao.BookRepository;
import cn.jly.spring.annotation.ioc.service.BookService;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import java.util.Map;

/**
 * @author lanyangji
 * @description 自动装配测试
 * @date 2020/11/13 上午 10:59
 */
public class IocTestAutowired {

    AnnotationConfigApplicationContext applicationContext;

    @Before
    public void setUp() throws Exception {
        applicationContext = new AnnotationConfigApplicationContext(MainConfigAutowired.class);
    }

    @After
    public void tearDown() throws Exception {
        applicationContext.close();
    }

    @Test
    public void test() {
        final BookService bookService = applicationContext.getBean(BookService.class);
        bookService.printBookRepository();

        final String label = bookService.getBookRepository().getLabel();
        System.out.println("label = " + label);

        final Map<String, BookRepository> map = applicationContext.getBeansOfType(BookRepository.class);
        for (Map.Entry<String, BookRepository> entry : map.entrySet()) {
            System.out.println(entry.getKey() + "---> " + entry.getValue() + "---> " + entry.getValue().getLabel()
            + "---> " + entry.getValue().hashCode());
        }
    }
}
